import React, {Component} from "react";
import {Card, Form, Modal} from "antd";
import PropTypes from "prop-types";
import {BtnsTableVED, EditFormNormal, SearchForm, TableContent} from "../../../widgets";
import {HTTP} from "../../../utils";
import {appInstanceAdd, appInstanceDel, appInstanceList} from "../../../utils/api";
import BreadcrumbCustom from "../../../components/BreadcrumbCustom";

const FormItem = Form.Item;



export default class extends Component {

    static contextTypes = {
        router: PropTypes.object
    }

    constructor(props) {
        super(props);
        this.table = undefined
        this.state = {
            searchParam: {appDomain: 'WINDER-PRODUCER'},
            formVisible: false,
            formReadOnly: false,
            formInitData: {},
        }
    }

    seaFields = [{
        label: '厂方名称',
        key: 'name'
    }]

    seaBtns = [{
        label: '新增',
        onClick: () => this.invokeAdd()
    }]


    columns = [{
        title: '厂方名称',
        dataIndex: 'name',
        key: 'name',
    }, {
        title: '机构编码',
        dataIndex: 'organNo',
        key: 'organNo',
    }]

    render() {
        return (
            <div>
                <BreadcrumbCustom first="业务管理" second="厂方管理"/>
                <Card title="厂方" bordered={false}>
                    <SearchForm fields={this.seaFields} btns={this.seaBtns} onSearch={this.onSearch}/>
                    <TableContent
                        ref={(e) => {this.table = e}}
                        columns={this.columns}
                        url = {appInstanceList}
                        searchParam={this.state.searchParam}
                    />
                </Card>
                {
                    this.state.formVisible && (
                        <EditFormNormal
                            initData={this.state.formInitData}
                            fields={this.editFields}
                            onClose={() => this.hideForm()}
                            readOnly={this.state.formReadOnly}
                            onSave={this.invokeFormSubmit}
                        />
                    )
                }
            </div>
        )
    }

    onSearch = (values) => {
        this.setState({
            searchParam: Object.assign({}, this.state.searchParam, values)
        }, () => {
            this.table.loadData()
        })
    }

    invokeAdd = (record) => {
        this.setState({
            formReadOnly: false
        }, () => {
            this.showForm()
        })
    }

    invokeView = (record) => {
        this.setState({
            formReadOnly: true
        }, () => {
            this.showForm(record)
        })
    }

    invokeEdit = (record) => {
        this.setState({
            formReadOnly: false
        }, () => {
            this.showForm(record)
        })
    }

    invokeDelete = (record) => {
        HTTP.post(appInstanceDel, record, () => {}, (resp) => {
            this.table.loadData()
        })
    }

    showForm = (d) => {
        this.setState({
            formVisible: true,
            formInitData: d || {}
        })
    }

    hideForm = () => {
        this.setState({
            formVisible: false,
            formInitData: {}
        })
    }

    invokeFormSubmit = (params, callback) => {
        HTTP.post(appInstanceAdd, params, () => {}, (resp) => {
            Modal.success({
                title: '提示',
                content: '保存成功'
            })
            callback && callback()
            this.hideFormSuccess()
        })
    }

    hideFormSuccess = () => {
        this.setState({
            formVisible: false,
            formInitData: {}
        }, () => {
            this.table.loadData()
        })
    }
}
